package com.alib.springcloud.controller;


import com.alib.springcloud.ribbonrule.ImpleRoundRobinRule;
import com.alib.springcloud.entities.CommonResult;
import com.alib.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class OrderController {

    @Autowired // 实质上是 Ribbon 的服务发现
    RestTemplate restTemplate;

    @Autowired //自己手写实现的算法
    private ImpleRoundRobinRule ir;

    @Autowired //Fegin服务发现
    DiscoveryClient discoveryClient;

    // 这里不再单独访问8001
    // 而是访问 eureka 暴露的服务名，进而通过 负载均衡 访问服务
    public static final String PAYMENT_URL = "http://CLOUD-PROVIDER-PAYMENT";

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment){
        log.info("***消费者***");
        log.info("***** 插入成功！插入数据：" + payment.toString());

        return  restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        log.info("***消费者***");
        log.info("***** 查询成功！查询ID：" + id);

        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/"+id, CommonResult.class);
    }

    @GetMapping("/consumer/payment/getentity/{id}")
    public CommonResult<Payment> getEntityPaymentById(@PathVariable("id") Long id){

        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/"+id, CommonResult.class);
        log.info("服务注册成！输出entity:" + entity.toString());
        log.info("服务注册成！输出entity.getBody():" + entity.getBody().toString());

        if( entity.getStatusCode().is2xxSuccessful() ){
            return entity.getBody();
        }else{
            return new CommonResult(400,"发现服务失败!!1");
        }
    }

    @GetMapping("/consumer/payment/lb")
    public String lbTest(){
        // 遍历 eureka 中所有已注册的服务
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        if(instances.size() <= 0){
            return "eureka 没有发现，已注册的服务。";
        }
        // 通过自定义算法，确定调用某个服务的url。
        URI uri = ir.getServiceInstance(instances).getUri();
        // RestTemplate 呼叫服务
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }


    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        String result1 = restTemplate.getForObject("http://localhost:8002"+"/payment/zipkin/", String.class);
        return result + result1;
    }

}
